home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / mg2a_src.zip / TERMLIB / TGOTO.OLD < prev    next >
Text File  |  1988-08-23  |  7KB  |  269 lines

  1. /************************************************************************
  2.  *                                      *
  3.  *              Copyright (c) 1982, Fred Fish              *
  4.  *              All Rights Reserved                  *
  5.  *                                      *
  6.  *    This software and/or documentation is released for public          *
  7.  *    distribution for personal, non-commercial use only.          *
  8.  *    Limited rights to use, modify, and redistribute are hereby      *
  9.  *    granted for non-commercial purposes, provided that all          *
  10.  *    copyright notices remain intact and all changes are clearly     *
  11.  *    documented.  The author makes no warranty of any kind with      *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular     *
  14.  *    purpose.                                  *
  15.  *                                      *
  16.  ************************************************************************
  17.  */
  18. /*
  19.  * Modified:
  20.  *    1 May 86 ...!ihnp4!ut-sally!ut-ngp!mic
  21.  *          Now forces a '\0' at end of tgoto string.     Tgoto wasn't,
  22.  *          and this screwed up VT100-style (i.e. variable) cursor
  23.  *          addressing.
  24.  */
  25.  
  26.  
  27.  
  28. /*
  29.  *  LIBRARY FUNCTION
  30.  *
  31.  *    tgoto   expand cursor addressing string from cm capability
  32.  *
  33.  *  KEY WORDS
  34.  *
  35.  *    termcap
  36.  *
  37.  *  SYNOPSIS
  38.  *
  39.  *    char *tgoto(cm,destcol,destline)
  40.  *    char *cm;
  41.  *    int destcol;
  42.  *    int destline;
  43.  *
  44.  *  DESCRIPTION
  45.  *
  46.  *    Returns cursor addressing string, decoded from the cm
  47.  *    capability string, to move cursor to column destcol on
  48.  *    line destline.
  49.  *
  50.  *    The following sequences uses one input argument, either
  51.  *    line or column, and place the appropriate substitution
  52.  *    in the output string:
  53.  *
  54.  *          %d      substitute decimal value (in ASCII)
  55.  *          %2      like %d but forces field width to 2
  56.  *          %3      like %d but forces field width to 3
  57.  *          %.      like %c
  58.  *          %+x     like %c but adds ASCII value of x
  59.  *
  60.  *    The following sequences cause processing modifications
  61.  *    but do not "use up" one of the arguments.    If they
  62.  *    act on an argument they act on the next one to
  63.  *    be converted.
  64.  *
  65.  *          %>xy    if next value to be converted is
  66.  *              greater than value of ASCII char x
  67.  *              then add value of ASCII char y.
  68.  *          %r      reverse substitution of line
  69.  *              and column (line is substituted
  70.  *              first by default).
  71.  *          %i      causes input values destcol and
  72.  *              destline to be incremented.
  73.  *          %%      gives single % character in output.
  74.  *
  75.  *  BUGS
  76.  *
  77.  *    Does not implement some of the more arcane sequences for
  78.  *    radically weird terminals (specifically %n, %B, & %D).
  79.  *    If you have one of these you deserve whatever happens.
  80.  *
  81.  */
  82.  
  83.  
  84.  
  85. /*
  86.  *    Miscellaneous stuff
  87.  */
  88.  
  89. #include <stdio.h>
  90.  
  91. #define MAXARGS 2
  92.  
  93. static char *in;          /* Internal copy of input string pointer */
  94. static char *out;          /* Pointer to output array */
  95. static int args[MAXARGS];     /* Maximum number of args to convert */
  96. static int pcount;          /* Count of args processed */
  97. static char output[64];              /* Converted string */
  98.  
  99.  
  100.  
  101.  
  102. /*
  103.  *  PSEUDO CODE
  104.  *
  105.  *    Begin tgoto
  106.  *      If no string to process then
  107.  *          Return pointer to error string.
  108.  *      Else
  109.  *          Initialize pointer to input string.
  110.  *          Initialize pointer to result string.
  111.  *          First arg is line number by default.
  112.  *          Second arg is col number by default.
  113.  *          No arguments processed yet.
  114.  *          While there is another character to process
  115.  *          If character is a not a % character then
  116.  *              Simply copy to output.
  117.  *          Else
  118.  *              Process the control sequence.
  119.  *          End if
  120.  *          End while
  121.  *          Return pointer to static output string.
  122.  *      End if
  123.  *    End tgoto
  124.  *
  125.  */
  126.  
  127. char *tgoto(cm,destcol,destline)
  128. char *cm;
  129. int destcol;
  130. int destline;
  131. {
  132.     static int process();
  133.  
  134.     if (cm == NULL) {
  135.       return("OOPS");
  136.     } else {
  137.       in = cm;
  138.       out = output;
  139.       args[0] = destline;
  140.       args[1] = destcol;
  141.       pcount = 0;
  142.       while (*in != NULL) {
  143.       if (*in != '%') {
  144.           *out++ = *in++;
  145.       } else {
  146.           process();
  147.       }
  148.       }
  149.       *out = NULL;    /* Just to make sure */
  150.       return(output);
  151.     }
  152. }
  153.  
  154.  
  155.  
  156. /*
  157.  *  INTERNAL FUNCTION
  158.  *
  159.  *    process    process the conversion/command sequence
  160.  *
  161.  *  SYNOPSIS
  162.  *
  163.  *    static process()
  164.  *
  165.  *  DESCRIPTION
  166.  *
  167.  *    Processes the sequence beginning with the % character.
  168.  *    Directly manipulates the input string pointer, the
  169.  *    output string pointer, and the arguments.     Leaves
  170.  *    the input string pointer pointing to the next character
  171.  *    to be processed, and the output string pointer pointing
  172.  *    to the next output location.  If conversion of
  173.  *    one of the numeric arguments occurs, then the pcount
  174.  *    is incremented.
  175.  *
  176.  */
  177.  
  178.  
  179.  
  180. /*
  181.  *  PSEUDO CODE
  182.  *
  183.  *    Begin process
  184.  *      Skip over the % character.
  185.  *      Switch on next character after %
  186.  *      Case 'd':
  187.  *          Process %d type conversion (variable width).
  188.  *          Reinitialize output pointer.
  189.  *          Break;
  190.  *      Case '2':
  191.  *          Process %d type conversion (width 2).
  192.  *          Reinitialize output pointer.
  193.  *          Break;
  194.  *      Case '3':
  195.  *          Process %d type conversion (width 3).
  196.  *          Reinitialize output pointer.
  197.  *          Break;
  198.  *      Case '.'
  199.  *          Process %c type conversion.
  200.  *          Break;
  201.  *      Case '+':
  202.  *          Process %c type conversion with offset.
  203.  *          Break;
  204.  *      Case '>':
  205.  *          Process argument modification.
  206.  *          Break;
  207.  *      Case 'r':
  208.  *          Process argument reversal.
  209.  *          Break;
  210.  *      Case 'i':
  211.  *          Increment argument values.
  212.  *          Break;
  213.  *      Case '%':
  214.  *          Copy to output, incrementing pointers.
  215.  *          Break;
  216.  *      End switch
  217.  *    End process
  218.  *
  219.  */
  220.  
  221.  
  222.  
  223.  
  224. static process()
  225. {
  226.     int temp;
  227.  
  228.     in++;
  229.     switch(*in++) {
  230.     case 'd':
  231.       sprintf(out,"%d",args[pcount++]);
  232.       out = &output[strlen(output)];
  233.       break;
  234.     case '2':
  235.       sprintf(out,"%02d",args[pcount++]);
  236.       out = &output[strlen(output)];
  237.       break;
  238.     case '3':
  239.       sprintf(out,"%03d",args[pcount++]);
  240.       out = &output[strlen(output)];
  241.       break;
  242.     case '.':
  243.       *out++ = args[pcount++];
  244.       break;
  245.     case '+':
  246.       *out++ = args[pcount++] + *in++;
  247.       break;
  248.     case '>':
  249.       if (args[pcount] > *in++) {
  250.       args[pcount] += *in++;
  251.       } else {
  252.       in++;
  253.       }
  254.       break;
  255.     case 'r':
  256.       temp = args[pcount];
  257.       args[pcount] = args[pcount+1];
  258.       args[pcount+1] = temp;
  259.       break;
  260.     case 'i':
  261.       args[pcount]++;
  262.       args[pcount+1]++;
  263.       break;
  264.     case '%':
  265.       *out++ = '%';
  266.       break;
  267.     }
  268. }
  269.